<?php
	function f310($sql){
		//data handle instance
		//set request
		//define format
		//set format
		//define query string
		//process time
		//process fitler
		$dataHandle = new DataHandle();
		$dataHandle->setRequest("310");

		$dataType = getValue($_GET['dataType']);
		$action = getValue($_GET['action']);

		if ($dataType == "json"){
			$error = $dataHandle->processTime($startTime, $endTime);
			if($error == ""){
				$filter = $dataHandle->processFilters($startTime, $endTime, "silo_DayLevels_view","day_date");
				$query = getJSONQuery($startTime, $endTime, $filter);
				print($dataHandle->printJSON($sql, $query));
			}
			else
				print($error);
		}
		else if($action == "getYears"){
			$query = getYears();
			print($dataHandle->printJSON($sql, $query));
		}
		else{
			$temp = array();
			$temp['error'] = "310: Bad request";
			print(json_encode($temp));
		}

		unset($dataHandle);

	}

	function getJSONQuery(&$startTime, &$endTime, &$filter){
		$queryString = "
			SELECT
				day_date_formatted + ' ' + day_name AS 'Date',
				silo_name AS 'Silo',
				day_start_level AS 'startDay',
				day_end_level AS 'endDay',
				usage AS 'Usage',
				delivered AS 'Delivered'
			FROM silo_DayLevels_view ";

		$queryString = $queryString.$filter;
		$queryString = $queryString." order by day_date desc;";

		return $queryString;
	}
	function getYears(){
		$queryString = 
			"SELECT DISTINCT 
				DATEPART(year, day_date) AS availableYear 
			FROM silo_DayLevels_view 
			ORDER BY availableYear desc";
		return $queryString;
	}
?>